//准许权限控制
/* 网络请求 */
// import http from "@/utils/axios";

/* 这个是为了请求本地json文件-------临时使用 */
import http from "axios";
import { constantRoutes } from "@/router";
import { registerApps } from "@/qiankun";
const permission = {
  namespaced: true,
  state: () => ({
    routers: null,
    mainMenu: [
    ],
    menuList: {
      base : [
        {
          "title": "全局数据展示",
          "moduleName": "base",
          "name": "basehome",
          "path": "/base/",
          "component": "/",
          "children": [],
          "icon": "home",
          "meta": { "isTabs": true, "isSide": true }
        },
        {
          "title": "页面展示",
          "moduleName": "base",
          "name": "sbaseabout",
          "path": "/base/about",
          "component": "/about",
          "children": [],
          "icon": "home",
          "meta": { "isTabs": true, "isSide": true }
        }
      ]
    },
    subMenu: [],
    currentModuleName: "home",
    currentPage: null,
    permission: [],
  }),
  mutations: {
    UPDATE_CURRENT_MODULE_NAME(state, payload) {
      sessionStorage.setItem("currentApp", payload);
      state.currentModuleName = payload;
    },
    UPDATE_CURRENT_PAGE(state, payload) {
      sessionStorage.setItem("currentPage", payload);
      state.currentPage = payload;
    },
    UPDATE_MENU_LIST(state, payload) {
      console.log("permission/UPDATE_MENU_LIST,payload=", payload);
      state.menuList = { ...state.menuList, ...payload};
      console.log("permission/UPDATE_MENU_LIST,state.menuList=", state.menuList);
    },
    UPDATE_MAIN_MENU(state, payload) {
      console.log("permission/UPDATE_MAIN_MENU", payload);
      registerApps();
      state.mainMenu = [
        ...[{
          moduleTitle: "微框架",
          moduleName: "microservice",
          icon: "微框架",
          name: "microservice",
          entry: "http://localhost:3000/",
          activeRule: "/",
        }],
        ...payload,
      ];
    },
    UPDATE_SUB_MENU(state, payload) {
      //state.subMenu = payload;
      state.subMenu = [];
      const homeMenuData = {
        title: "首页",
        moduleName: "Home",
        path: "/home",
        meta: { isTabs: false, isSide: false, moduleName: "main", title: "首页" },
      };
      if (typeof payload === "boolean") {
        state.subMenu.push(home·MenuData);
        return;
      }
      if (typeof payload === "object") {
        state.subMenu = payload;
      }
    },
    UPDATE_ROUTERS(state, payload) {
      state.routers = constantRoutes.concat(payload);
    },
    UPDATE_PERMISSION(state, payload) {
      state.permission = [...payload];
    },
  },
  actions: {
    generateRoutes({ commit }) {
      return new Promise((resolve) => {
        // 向后端请求路由数据
        http.get("/mock/menu.json").then((res) => {
          const data = res.data;
          // const data = res.data.data.list;
          commit("UPDATE_MENU_LIST", data);
          console.log('data=',data);
          let routes = [];
          for (let i = 0; i < data.length; i++) {
            const module = data[i];
            const route = getMenuItem(module.menuList);
            routes = [...routes, ...route];
          }
          // routes.push({
          //   path: "*",
          //   name: "notfound",
          //   component: () => import("@/views/404.vue"),
          // });
          commit("UPDATE_ROUTERS", routes);
          console.log('routes=',routes);
          resolve(routes);
        });
      });
    },
  },
  getters: {
    getPermission (state) {
      return state.permission;
    },
    getMenuList: (state) => {
      return state.menuList;
    },
  },
};

// // 组装路由数据
// function getMenuItem(menus) {
//   let routers = [];
//   menus.forEach((menu, index) => {
//     if (menu.children && menu.children.length > 0) {
//       routers = [...routers, ...getMenuItem(menu.children)];
//     } else {
//       const route = {
//         path: menu.path,
//         component: Layout,
//         name: menu.name,
//         meta: menu.meta,
//       };
//       route.meta.title = menu.title;
//       route.meta.moduleName = menu.moduleName;
//       routers.push(route);
//     }
//   });
//   // for (let index = 0; index < menus.length; index++) {
//   //   const menu = menus[index];
//   //   if (menu.children && menu.children.length > 0) {
//   //     routers = [...routers, ...getMenuItem(menu.children)];
//   //   } else {
//   //     const route = {
//   //       path: menu.path,
//   //       component: Layout,
//   //       name: menu.name,
//   //       meta: menu.meta,
//   //     };
//   //     route.meta.title = menu.title;
//   //     route.meta.moduleName = menu.moduleName;
//   //     routers.push(route);
//   //   }
//   // }
//   return routers;
// }

export default permission;
